# Stage 1: Build the application
FROM node:18-alpine AS builder

# Set the working directory
WORKDIR /app

# 设置 npm 镜像源
RUN npm config set registry https://registry.npmmirror.com

# 设置环境变量以跳过 puppeteer 的 Chromium 下载
# 这对于前端构建通常是安全的，除非你的项目直接使用了 puppeteer
ENV PUPPETEER_SKIP_DOWNLOAD=1

# Copy all source code (including package.json)
COPY . .

# Install dependencies
# Using --prefer-offline and --no-audit for speed and to avoid issues
# Skipping Chromium download for puppeteer
RUN npm install --prefer-offline --no-audit --progress=false

# Build the application for H5
RUN npm run build

# Stage 2: Serve the application with Nginx
FROM nginx:1.25-alpine

# Copy the built files from the builder stage
# The build output for H5 is expected to be in /app/dist/build/h5
COPY --from=builder /app/dist/build/h5 /usr/share/nginx/html

# Copy nginx configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf

# Expose port 80
EXPOSE 80

# Start Nginx
CMD ["nginx", "-g", "daemon off;"]
